import '../commons/common'

import './js/login'

import './my.less'

const phoneReg=/^1\d{10}$/

$('#page-main .item-link').click(function(){
    var title = $(this).find('.item-title').text()
    $('#detail-title').text(title)
})

$('#login-form').submit(function(){
    var phone = $('#input-login-phone').val(),
        password = $('#input-login-password').val();
    if(!phoneReg.test(phone)){
        return $.alert('手机格式不正确')
    }

    if(!password){
        return $.alert('密码不能为空')
    }

    $.ajax({
        url:'/login',
        type:'post',
        data:$(this).serialize(),
        success(e){
            if(e.error){
                return $.alert(e.error+'</br>'+e.reason)
            }
            window.location.reload(true)
        }

    })
    return false;
})

$('#register-form').submit(function(){
    var username = $('#input-reg-name').val(),
        phone = $('#input-reg-phone').val(),
        yzm = $('#input-reg-yzm').val(),
        password = $('#input-reg-password').val(),
        repassword = $('#input-reg-repassword').val();
    if(!username){
         $.alert('用户名不能为空')
        return false;
    }
    if(!phoneReg.test(phone)){
         $.alert('手机号格式有问题')
        return false;
    }
    if(password.length<6){
         $.alert('用户名不能少于6位')
        return false;
    }
    if(password!==repassword){
         $.alert("两次密码不一致")
        return false;
    }

    $.ajax({
        url:'/register',
        type:'post',
        data:$(this).serialize(),
        success(e){
            if(e.error){
                return $.alert(e.error)
            }
            window.location.reload(true)
        }
    })

    return false;
})


var curOrderNum=undefined;


$('body').on('click','.btn-post-comment',function(){
    var $this  =$(this);
    curOrderNum = $this.closest('.order-item').attr('data-order-num');
    //"{"hotelno":"sdcs","phone":"13254658588","evclass":"1","evContent":"很不错，环境挺好！","orderNum":"RS1512060009002"}"

    var modal = $.modal({
        title: '提交评论',
        afterText:  `
            <textarea id="modal-content" placeholder="您的评论" style="resize: none;width: 100%;margin-top: 5px;
                border: #f0f0f0;height: 100px;padding: 0 5px;font-size: 14px;"></textarea>`,
        buttons: [
            {
                text: '取消'
            },
            {
                text: '提交',
                bold: true,
                close:false,
                onClick: function () {
                    $.ajax({
                        url:'/commentOrder',
                        type:'post',
                        data:{
                            orderNum:curOrderNum,
                            hotelno:__hn,
                            evContent:$('#modal-content').val()
                        },
                        success(e){
                            $.closeModal(modal)
                            if(e.error){
                                return $.alert(e.error)
                            }
                            $.alert('评论成功')
                            $this.closest('.card-footer').remove();
                        },
                        error(){
                            $.closeModal(modal)
                            $.alert('未知错误,请稍后评论')
                        }
                    })
                }
            },
        ]
    })
})


$('body').on('click','.js-cancel-order',function(){
    var $orderItem = $(this).closest('.order-item')
    var orderNum = $orderItem.attr('data-order-num'),
        hotelno  = __hn;

    $.confirm('您确定要取消订单?',function(){
        $.ajax({
            type:'post',
            url:'/cancel-order',
            data:{
                orderNum:orderNum,
                hotelno:hotelno
            },
            success:function(e){
                if(e.error){
                    return $.alert(e.error +'.原因:'+e.reason)
                }
                $orderItem.fadeOut();
            },
            error:function(){
                $.alert('sorry!发生错误,请稍后重试')
            },
            complete:function(){
                $.hidePreloader()
            }
        })
    })
})





var selectRoomOrderNum = ''
$('.js-select-room').click(function(){
    //if(__g.hasSelectRoom){
    //    return $.alert('您已经选过房了')
    //}
    var $this = $(this)
    var orderNum = $this.attr('data-order-num'),
        rtCode = $this.attr('data-rt-code'),
        hotelno = __hn;

    selectRoomOrderNum = orderNum
    var roomCount = $this.attr('data-room-count')
    $('#select-room-list').attr('data-room-count',roomCount)
    $('#select-room-count').text(`您可以选择${roomCount}间房`);
    $.showPreloader('正在请求房间列表,请稍后')
    $.ajax({
        type:'post',
        url:'/select-room',
        data:{
            orderNum:orderNum,
            hotelno:hotelno,
            rtCode:rtCode
            //orderNum:'RS1512070009003',
            //hotelno:hotelno,
            //rtCode:'BR'
        },
        success(e){
            var roomList = e.roomList
            var html = ''
            for(var i in roomList){
                var room = roomList[i];
                html+=`<div class="room-wrapper">
                    <div class="room-number">${room.rmNum}</div>
                </div>`
            }
            $('#select-room-list').html(html)
            var curPage = $('.current-page'),
                nextPage = $('#page-select-room');
            $.myRouter.forward(nextPage,curPage)
        },
        error:function(){
            $.alert('sorry!发生错误,请稍后重试')
        },
        complete:function(){
            $.hidePreloader()
        }
    })
})
//$('.js-select-room').trigger('click')

$('#select-room-list').on('click','.room-wrapper',function(){
    $(this).toggleClass('active')
})



$('#submit-select-room').click(function(){
    var selectList = []
    $('.room-wrapper.active').each(function(){
        selectList.push($(this).find('.room-number').text())
    })
    if(selectList.length==0){
        return $.alert('您还没有选择房间')
    }
    var maxCount = $('#select-room-list').attr('data-room-count')*1;
    if(selectList.length>maxCount){
        return $.alert(`您预订了${maxCount}间房,选房数不能超过.`)
    }
    var orderNum = $('.js-s-order-num').text(),
        hotelno = __hn;

    $.showPreloader('选房提交中,请稍后')

    $.ajax({
        type:'post',
        url:'/submit-room',
        data:{
            orderNum:selectRoomOrderNum,//'RS1512070009003',
            hotelno:hotelno,
            rooms:selectList.join(',')
        },
        success(e){
            if(e.error){
                return $.alert(e.error +'.原因:'+e.reason)
            }
            //__g.hasSelectRoom = true
            var nextPage = $('#page-order-success'),
                curPage = $('#page-select-room');
            $.myRouter.back(nextPage,curPage)
            $.alert(`选房成功,请在${e.times}分钟内进行付款`);
        },
        error:function(){
            $.alert('sorry!发生错误,请稍后重试')
        },
        complete:function(){
            $.hidePreloader()
        }

    })
})